import { HubConnectionBuilder, LogLevel } from '@microsoft/signalr'

export const createConsumer = (websocketURL, authToken) => {
  const connection = new HubConnectionBuilder()
    .withUrl(websocketURL,
      {
        accessTokenFactory: () => authToken
      })
    .configureLogging(LogLevel.Information)
    .build()

  connection.start()
  connection.onclose(async () => {
    console.log('onclose')
    await start()
  })
  async function start () {
    try {
      await connection.start()
      console.log('SignalR Connected.')
    } catch (err) {
      console.log(err)
      setTimeout(start, 5000)
    }
  }

  return connection
}
